<?php
/***********************************************************
	Filename: app/admin/control/open.php
	Note	: 弹出窗口中的信息管理
	Version : 3.0
	Author  : qinggan
	Update  : 2009-11-24
***********************************************************/
class open_c extends Control
{
	var $type_video = array("wma","mp3","wmv","asf","mpg","mpeg","avi","asx","rm","rmvb","ram","ra","swf","flv","dat");
	var $type_img = array("jpg","gif","png","jpeg");
	var $type_file = array("zip","rar","txt","tgz","tar","gz");
	function __construct()
	{
		parent::Control();
		$this->load_model("upfile");//读取附件操作类
		$this->tpl->assign("type_video",$this->type_video);
		$this->tpl->assign("type_img",$this->type_img);
		$this->tpl->assign("type_file",$this->type_file);
	}

	function open_c()
	{
		$this->__construct();
	}

	//读取列表
	function index_f()
	{
		$input_id = $this->trans_lib->safe("input");
		if(!$input_id) $input_id = "thumb_id";
		$input_view = $this->trans_lib->safe("view");
		if(!$input_view) $input_view = "thumb_view";
		$input_type = $this->trans_lib->safe("type");
		if($input_type != "video" && $input_type != "img" && $input_type != "download")
		{
			$input_type = "img";
		}
		$this->tpl->assign("input_id",$input_id);
		$this->tpl->assign("input_view",$input_view);
		$page_url = site_url("open")."input=".rawurlencode($input_id)."&";
		$page_url.= "view=".rawurlencode($input_view)."&type=".rawurlencode($input_type)."&";
		if($input_type == "video")
		{
			$condition = "ftype IN('".implode("','",$this->type_video)."')";
			$this->upfile_m->set_condition($condition);
			//设置上传的附件类型
			$tmp_array = array();
			foreach($this->type_video AS $key=>$value)
			{
				$tmp_array[] = "*.".$value;
			}
			$this->tpl->assign("swfupload_filetype",implode(";",$tmp_array));
			$this->tpl->assign("swfupload_note","Video Files");
		}
		elseif($input_type == "img")
		{
			$condition = "ftype IN('".implode("','",$this->type_img)."')";
			$this->upfile_m->set_condition($condition);
			$tmp_array = array();
			foreach($this->type_img AS $key=>$value)
			{
				$tmp_array[] = "*.".$value;
			}
			$this->tpl->assign("swfupload_filetype",implode(";",$tmp_array));
			$this->tpl->assign("swfupload_note","Image Files");
		}
		else
		{
			$this->tpl->assign("swfupload_filetype","*.*");
			$this->tpl->assign("swfupload_note","All Files");
		}
		//查看postdate数据
		$page_url_2 = $page_url;
		$postdate = $this->trans_lib->safe("postdate");
		if($postdate)
		{
			$condition = "postdate>='".strtotime($postdate)."'";
			$this->upfile_m->set_condition($condition);
			$page_url_2 .= "postdate=".rawurlencode($postdate)."&";
		}
		$keywords = $this->trans_lib->safe("keywords");
		if($keywords)
		{
			$condition = "(title LIKE '%".$keywords."%' OR filename LIKE '%".$keywords."%')";
			$this->upfile_m->set_condition($condition);
			$page_url_2 .= "keywords=".rawurlencode($keywords)."&";
		}
		$total = $this->upfile_m->get_count();//取得总数
		$pagelist = $this->page_lib->page($page_url_2,$total);
		$this->tpl->assign("pagelist",$pagelist);
		$pageid = $this->trans_lib->int(SYS_PAGEID);
		$rslist = $this->upfile_m->get_list($pageid);
		$this->tpl->assign("rslist",$rslist);
		$this->tpl->assign("page_url",$page_url);
		//加载模板
		$tplfile = "list_".$input_type.".html";
		if($input_type == "img" && $input_id == "thumb_id")
		{
			$tplfile = "list_thumb.html";
		}
		$this->tpl->display("open/".$tplfile);
	}

	//通过swf+php进行附件上传，支持大文件
	function upload_f()
	{
		$post_max_size = ini_get('post_max_size');
		$unit = strtoupper(substr($post_max_size, -1));
		$multiplier = ($unit == 'M' ? 1048576 : ($unit == 'K' ? 1024 : ($unit == 'G' ? 1073741824 : 1)));
		$max_post = $multiplier * intval($post_max_size);
		$content_length = intval($_SERVER["CONTENT_LENGTH"]);
		//最大上传限制
		if($post_max_size && $content_length>$max_post)
		{
			header("HTTP/1.1 500 Internal Server Error");
			exit("POST exceeded maximum allowed size.");
		}

		$upload_name = "Filedata";//指定上传目录名
		//存储目录
		//PHPOK附件目录使用年月/日结构，如果不支持，将启用当前目录
		if(!defined("SYS_UP_PATH")) define("SYS_UP_PATH","upfiles");
		//附件目录
		$save_path = ROOT.SYS_UP_PATH.date("/Ym/d/");
		$this->file_lib->make($save_path);//创建存储目录
		if(!file_exists($save_path))
		{
			$save_path = ROOT.SYS_UP_PATH."/";
		}
		//最大2G文件上传
		$max_file_size_in_bytes = 2147483647;
		//支持的附件类型
		$extension_whitelist = array_merge($this->type_video,$this->type_img,$this->type_file);
		//文件名长度，不能超过250个字符
		$MAX_FILENAME_LENGTH = 250;
		//新文件名称
		$file_name = substr(md5(time().rand(0,9999)),9,16);
		$zip_filename = $file_name;//如果是zip压缩包
		$file_extension = "";
		$uploadErrors = array(0=>"There is no error, the file uploaded with success",
			1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
			2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
			3=>"The uploaded file was only partially uploaded",
			4=>"No file was uploaded",
			6=>"Missing a temporary folder");
		if(!isset($_FILES[$upload_name]))
		{
			exit("No upload found in \$_FILES for " . $upload_name);
		}
		elseif(isset($_FILES[$upload_name]["error"]) && $_FILES[$upload_name]["error"] != 0)
		{
			exit($uploadErrors[$_FILES[$upload_name]["error"]]);
		}
		elseif(!isset($_FILES[$upload_name]["tmp_name"]) || !@is_uploaded_file($_FILES[$upload_name]["tmp_name"]))
		{
			exit("Upload failed is_uploaded_file test.");
		}
		elseif(!isset($_FILES[$upload_name]['name']))
		{
			exit("File has no name.");
		}

		//判断附件大小
		$file_size = @filesize($_FILES[$upload_name]["tmp_name"]);
		if (!$file_size || $file_size > $max_file_size_in_bytes)
		{
			exit("File exceeds the maximum allowed size");
		}
		if ($file_size <= 0)
		{
			exit("File size outside allowed lower bound");
		}

		//判断附件类型是否符合要求
		$path_info = pathinfo($_FILES[$upload_name]['name']);
		$file_extension = strtolower($path_info["extension"]);

		$file_name .= ".".$file_extension;
		//附件名称
		$tmp_title = basename($_FILES[$upload_name]['name']);

		if(!@copy($_FILES[$upload_name]["tmp_name"],$save_path.$file_name))
		{
			exit("File could not be saved.");
		}

		if(!in_array($file_extension,$extension_whitelist))
		{
			if(function_exists("gzcompress"))
			{
				//生成zip压缩包
				$this->load_lib("zip");
				$tmp_contents = $this->file_lib->cat($save_path.$file_name);
				$this->zip_lib->addFile($tmp_contents,$tmp_title);
				unset($tmp_contents);
				//存储
				$this->zip_lib->output($save_path.$zip_filename.".zip");
				$this->file_lib->rm($save_path.$file_name);//删除上传的文件
			}
			else
			{
				//如果不支持在线压缩，则重命名上传的附件为zip
				$this->file_lib->mv($save_path.$file_name,$save_path.$file_name.".link.zip");
			}
			$file_extension = "zip";
			$file_name = $zip_filename.".zip";
		}

		$array = array();
		//存储数据
		$array["title"] = $tmp_title;
		$array["filename"] = str_replace(ROOT,"",$save_path.$file_name);
		$array["postdate"] = time();
		$array["ftype"] = $file_extension;
		//存储
		$insert_id = $this->upfile_m->save($array);
		if(!$insert_id)
		{
			$this->file_lib->rm($save_path.$file_name);
			exit("File cold not be saved.");
		}

		//判断是否需要生成缩略图
		if(!in_array($file_extension,$this->type_img))
		{
			exit(0);
		}
		//[生成水印图片]
		$this->load_lib("gd");
		$thumbfile = $this->gd_lib->thumb($save_path.$file_name,$insert_id);
		if($thumbfile)
		{
			//[存储数据]
			$update_array = array();
			$update_array["thumb"] = str_replace(ROOT,"",$save_path).$thumbfile;
			$this->upfile_m->save($update_array,$insert_id);
		}

		//判断生成其他图片套件
		$this->_gd_create($insert_id);
		exit(0);
	}

	//临时图片批量生成工具
	function pl_f()
	{
		$picid = $this->trans_lib->safe("picid");
		$now_id = $this->trans_lib->int("pid");
		if($picid == "all")
		{
			if(!$now_id)
			{
				$rs = $this->upfile_m->get_one_c("ftype IN('".implode("','",$this->type_img)."')");
				if(!$rs)
				{
					error("批量生成图片错误，没有取得一张有效图片");
				}
				$now_id = $rs["id"];
			}
		}
		else
		{
			if(!$picid)
			{
				error("没有指定要生成的图片ID");
			}
			$pic_array = explode(",",$picid);
			if(!$now_id)
			{
				$now_id = $pic_array[0];
			}
		}
		//重新批量生成新图片信息
		$this->_gd_create($now_id);
		$tmp_rs = $this->upfile_m->get_one($now_id);
		//取得下一张图片信息
		$set_url = site_url("open,pl")."picid=".$picid."&pid=";
		if($picid == "all")
		{
			//[取得下一张图片ID]
			$condition = "ftype IN('".implode("','",$this->type_img)."') AND id>'".$now_id."'";
			$next_rs = $this->upfile_m->get_one_c($condition);
			if(!$next_rs)
			{
				error("图片批量生成完毕");
			}
			else
			{
				$next_id = $next_rs["id"];
				$set_url.= $next_id;
				error("请稍候，正在更新中，已更新至：".$tmp_rs["title"],$set_url);
			}
		}
		else
		{
			$next_id = 0;
			foreach($pic_array AS $key=>$value)
			{
				if($value == $now_id)
				{
					$next_id = $pic_array[$key+1];
					break;
				}
			}
			if(!$next_id)
			{
				error("图片批量生成完毕");
			}
			else
			{
				$set_url.= $next_id;
				//echo $set_url;
				error("请稍候，正在更新中，已更新至：".$tmp_rs["title"],$set_url);
			}
		}
	}

	//水印图片生成
	function _gd_create($picid)
	{
		@set_time_limit(0);#[设置防止超时]
		$this->load_lib("gd");
		$this->load_model("gdtype");
		$gdlist = $this->gdtype_m->get_all(1);
		if(!$gdlist)
		{
			return false;
		}
		$rs = $this->upfile_m->get_one($picid);
		if(!$rs || !in_array($rs["ftype"],$this->type_img))
		{
			return false;
		}
		$gd_rs = $this->upfile_m->get_one_gd($picid);
		if($gd_rs)
		{
			$rslist = array();
			foreach($gd_rs AS $key=>$value)
			{
				$rslist[$value["gdtype"]] = $value["id"];
			}
		}
		foreach($gdlist AS $key=>$value)
		{
			$quality = $value["quality"] ? $value["quality"] : 80;
			$this->gd_lib->Set("quality",$quality);#[设置图片质量]
			$cuttype = $value["cuttype"] ? true : false;
			$this->gd_lib->SetCut($cuttype);
			$this->gd_lib->Filler(intval($value["border"]),$value["bordercolor"],$value["bgcolor"],$value["bgimg"],intval($value["padding"]));
			$iscopyright = ($value["water"] && file_exists(ROOT.$value["water"])) ? true : false;
			$this->gd_lib->iscopyright($iscopyright);
			$this->gd_lib->CopyRight($value["water"],$value["picposition"],$value["trans"]);
			$newfile = $value["pictype"]."_".$picid;#[新图片名称]
			$width = intval($value["width"]);
			if(!$width)
			{
				$width = 100;
			}
			$height = intval($value["height"]);
			if(!$height)
			{
				$height = 100;
			}
			$newpic = $this->gd_lib->Create($rs["filename"],$newfile,$width,$height);
			$array = array();
			$array["gdtype"] = $value["pictype"];
			$array["pid"] = $picid;
			$array["filename"] = str_replace(basename($rs["filename"]),basename($newpic),$rs["filename"]);
			$_tmp_id = $rslist[$value["pictype"]];
			$this->upfile_m->save_gd($array);
		}
		return true;
	}

	//通过Ajax预览图片
	function ajax_preview_img_f()
	{
		$idstring = $this->trans_lib->safe("idstring");
		if(!$idstring)
		{
			exit("empty");
		}
		$rslist = $this->upfile_m->piclist($idstring);
		exit($this->json_lib->encode($rslist));
	}

	//通过Ajax来执行图片排序
	function sort_order_f()
	{
		$order_list = $this->trans_lib->safe("taxis");//数组排序
		if(!$order_list)
		{
			exit("error");
		}
		natsort($order_list);//排序
		$keys = array_keys($order_list);
		$string = sys_id_string($keys);
		exit($string);
	}

	//预览图片
	function preview_f()
	{
		$id = $this->trans_lib->int("id");
		if(!$id)
		{
			error("没有选择要预览的ID");
		}
		$rs = $this->upfile_m->get_one($id);
		$this->tpl->assign("rs",$rs);
		$ftype = "file";
		if(in_array($rs["ftype"],$this->type_img))
		{
			$rslist = $this->upfile_m->get_one_gd($id);
			$this->tpl->assign("rslist",$rslist);
			$ftype = "img";
		}
		if(in_array($rs["ftype"],$this->type_video))
		{
			$ftype = "video";
		}
		//判断是否预览
		$pretype = $this->trans_lib->safe("pretype");
		if($pretype && $pretype == "download")
		{
			$ftype = "file";
		}
		$this->tpl->assign("ftype",$ftype);
		$this->tpl->display("open/preview.html");
	}

	function fck_f()
	{
		$fck_id = $this->trans_lib->safe("fck_id");
		if(!$fck_id) $fck_id = "content";
		$input_type = $this->trans_lib->safe("type");
		if($input_type != "video" && $input_type != "img" && $input_type != "download")
		{
			$input_type = "img";
		}
		$this->tpl->assign("fck_id",$fck_id);
		$this->tpl->assign("input_type",$input_type);
		$page_url = site_url("open,fck")."fck_id=".rawurlencode($fck_id)."&";
		$page_url.= "type=".rawurlencode($input_type)."&";
		if($input_type == "video")
		{
			$condition = "ftype IN('".implode("','",$this->type_video)."')";
			$this->upfile_m->set_condition($condition);
			//设置上传的附件类型
			$tmp_array = array();
			foreach($this->type_video AS $key=>$value)
			{
				$tmp_array[] = "*.".$value;
			}
			$this->tpl->assign("swfupload_filetype",implode(";",$tmp_array));
			$this->tpl->assign("swfupload_note","Video Files");
		}
		elseif($input_type == "img")
		{
			$condition = "ftype IN('".implode("','",$this->type_img)."')";
			$this->upfile_m->set_condition($condition);
			$tmp_array = array();
			foreach($this->type_img AS $key=>$value)
			{
				$tmp_array[] = "*.".$value;
			}
			$this->tpl->assign("swfupload_filetype",implode(";",$tmp_array));
			$this->tpl->assign("swfupload_note","Image Files");
		}
		else
		{
			$this->tpl->assign("swfupload_filetype","*.*");
			$this->tpl->assign("swfupload_note","All Files");
		}
		//查看postdate数据
		$page_url_2 = $page_url;
		$postdate = $this->trans_lib->safe("postdate");
		if($postdate)
		{
			$condition = "postdate>='".strtotime($postdate)."'";
			$this->upfile_m->set_condition($condition);
			$page_url_2 .= "postdate=".rawurlencode($postdate)."&";
		}
		$keywords = $this->trans_lib->safe("keywords");
		if($keywords)
		{
			$condition = "(title LIKE '%".$keywords."%' OR filename LIKE '%".$keywords."%')";
			$this->upfile_m->set_condition($condition);
			$page_url_2 .= "keywords=".rawurlencode($keywords)."&";
		}
		$total = $this->upfile_m->get_count();//取得总数
		$pagelist = $this->page_lib->page($page_url_2,$total);
		$this->tpl->assign("pagelist",$pagelist);
		$pageid = $this->trans_lib->int(SYS_PAGEID);
		$rslist = $this->upfile_m->get_list($pageid);
		$this->tpl->assign("rslist",$rslist);
		$this->tpl->assign("page_url",$page_url);
		//加载模板
		$tplfile = "fck_".$input_type.".html";
		//加载GD类型
		$this->load_model("gdtype");
		$gdlist = $this->gdtype_m->get_all();
		$this->tpl->assign("gdlist",$gdlist);
		$this->tpl->display("open/".$tplfile);
	}

	//获取图片信息，返回json字符串
	function fck_img_f()
	{
		$idstring = $this->trans_lib->safe("idstring");
		if(!$idstring)
		{
			exit("empty");
		}
		$gd_type = $this->trans_lib->safe("gd_type");
		$rslist = $this->upfile_m->pic_list($idstring,$gd_type);
		if(!$rslist)
		{
			exit("empty");
		}
		exit($this->json_lib->encode($rslist));
	}
}
?>